package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_city;
import cn.com.api.entity.ApiT_points_rule;
import cn.com.api.mapper.ApiT_points_ruleMapper;
import cn.com.api.service.ApiT_points_ruleService;
import cn.com.api.service.StartOnLoadService;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class T_points_ruleController{

	private static Logger logger = Logger.getLogger(T_points_ruleController.class);
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private ApiT_points_ruleService apiT_points_ruleService;
	@Autowired
	private ApiT_points_ruleMapper apiT_points_ruleMapper;
	
	@RequestMapping(value = "points_rule_list")
	public ModelAndView points_rule_list(HttpServletRequest request,
			HttpServletResponse response,PageInfo pageInfo) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_points_rule_list.jsp");
		String city_id = request.getParameter("city_id");
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("city_id", city_id);
		
		List<ApiT_points_rule> pointsRuleList = apiT_points_ruleService.searchPointsRuleList(conditions, pageInfo);
		mv.addObject("list", pointsRuleList);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("conditions", conditions);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	} 
	
	@RequestMapping(value = "points_rule_add_pre")
	public ModelAndView points_rule_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_points_rule_add.jsp");
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		return mv;
	} 
	
	@RequestMapping(value = "points_rule_addOrUpdate_pre")
	public ModelAndView points_rule_addOrUpdate_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_points_rule_add.jsp");
		
		String id = request.getParameter("id");
		ApiT_points_rule t_points_rule = apiT_points_ruleService.searchPointsRuleById(id);
		
		
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("t_points_rule", t_points_rule);
		return mv;
	} 
	
	@RequestMapping(value = "points_rule_addOrUpdate")
	public void points_rule_addOrUpdate(HttpServletRequest request,
			HttpServletResponse response,ApiT_points_rule t_points_rule) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			System.out.println("------操作前");
//			ApiT_points_rule t_points_rule = new ApiT_points_rule();
			String start_time = request.getParameter("start_time1");
			System.out.println("start_time-"+start_time);
			t_points_rule.setStatus(0);
			//添加 编辑前，先校验日期是否有冲突的
			List<ApiT_points_rule> list = apiT_points_ruleMapper.checkDate(t_points_rule);
			if(list==null || list.size()<1){
				apiT_points_ruleService.addOrUpdateRule(t_points_rule);
				System.out.println("------添加编辑成功");
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "该时间段有活动正在进行，活动时间冲突！");
				System.out.println("------活动时间冲突");
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		System.out.println(JsonUtil.object2json(map));
		out.close();
		
	} 
	
	@RequestMapping(value = "points_rule_delete")
	public void points_rule_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			String id = request.getParameter("id");
			apiT_points_ruleService.deletePointsRuleById(id);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}catch(Exception e){
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
		
	} 
	
}



